﻿using UnityEngine;
namespace Project
{
    /// <summary>
    /// 魔法书怪物类
    /// </summary>
    public class ChefMonMonster_AI : MonsterBase
    {
        public float shootingRange = 10f; // 射程距离
        public GameObject bulletPrefab; // 子弹预制体
        public float interval;//攻击间隔
        private float time;//计时器
        private void Start()
        {
            monRigi2D = GetComponent<Rigidbody2D>();
            playerTrans = GameObject.FindGameObjectWithTag("Player").transform;
        }
        private void Update()
        {
            if (playerTrans == null) return;

            float dis = Vector3.Distance(transform.position, playerTrans.position);
            if(dis > shootingRange)
            {
                MoveTowardsPlayer();
            }
            else 
            {
                Shoot();
            }
        }
        private void Shoot()
        {
            monRigi2D.velocity = Vector3.zero;
            time += Time.deltaTime;
            if(time >= interval)
            {
                time -= interval;
                //获取玩家的方向
                Vector3 dir = (playerTrans.position - transform.position).normalized;
                //生成子弹
                GameObject go = Instantiate(bulletPrefab, transform.position, Quaternion.Euler(dir));
                go.GetComponent<BaseBullet>().OnFire(dir);
            }
        }
    }
}
